package com.example.othree.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.othree.common.PageParam;
import com.example.othree.common.R;
import com.example.othree.entity.Drone;
import com.example.othree.service.DroneManagementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 无人机数据Controller
 * Created by zr on 2024/10/17.
 */
@RestController
@CrossOrigin
@RequestMapping("/api")
public class DroneManagementController {
    @Autowired
    private DroneManagementService droneManagementService;

    @GetMapping("/drone/list")
    public List<Drone> list(){
        return droneManagementService.list();
    }

    @PostMapping("/drone/saveBatch")
    @ApiOperation("批量添加")
    public R saveBatch(@RequestBody List<Drone> droneList){
        return droneManagementService.saveBatch(droneList) ? R.ok() : R.error();
    }

    @PostMapping("/drone/update")
    @ApiOperation("修改")
    public R update(@RequestBody Drone drone){
        return droneManagementService.updateById(drone) ? R.ok() : R.error();
    }

    @PostMapping("/drone/delete/{id}")
    public R delete(@PathVariable Integer id){
        return droneManagementService.removeById(id) ? R.ok() : R.error();
    }

    @PostMapping("/drone/listPage")
    @ApiOperation("查询、分页")
    public R listPage(@RequestBody PageParam pageParam){
        HashMap param = pageParam.getParam();
        String equipmentName = param.get("equipmentName").toString();
        String equipmentNumber = param.get("equipmentNumber").toString();
        String detectionTime1 = param.get("earliestDetectionTime").toString();
        String detectionTime2 = param.get("latestDetectionTime").toString();

        Page<Drone> page = new Page();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<Drone> lambdaQueryWrapper = new LambdaQueryWrapper();

        if(StringUtils.isNotBlank(equipmentName) && !"null".equals(equipmentName)){
            lambdaQueryWrapper.eq(Drone::getEquipmentName, equipmentName);
        }
        if(StringUtils.isNotBlank(equipmentNumber) && !"null".equals(equipmentNumber)){
            lambdaQueryWrapper.eq(Drone::getEquipmentNumber, equipmentNumber);
        }
        if(StringUtils.isNotBlank(detectionTime1) && StringUtils.isNotBlank(detectionTime2) && !"null".equals(detectionTime1) && !"null".equals(detectionTime2)){
            if(detectionTime1.compareTo(detectionTime2) == 0){
                lambdaQueryWrapper.eq(Drone::getDetectionTime, detectionTime1);
            } else if (detectionTime1.compareTo(detectionTime2)>0) {
                return R.error().message("时间区间选择错误！");
            }else {
                lambdaQueryWrapper.ge(Drone::getDetectionTime, detectionTime1); // 大于detectionTime1
                lambdaQueryWrapper.le(Drone::getDetectionTime, detectionTime2);
            }
        }

        IPage result = droneManagementService.PageList(page, lambdaQueryWrapper);

        return R.ok().data("result", result);
    }

}
